package comm.utill;

import java.util.Calendar;

/**
 * Created by junxin on 2018/7/8.
 */
public class Var {

    public final static class Type {

        public static final int NULL = 1;
        public static final int OBJECT = 2;
        public static final int BYTE = 3;
        public static final int SHORT = 4;
        public static final int INT = 5;
        public static final int LONG = 6;
        public static final int FLOAT = 7;
        public static final int DOUBLE = 8;
        public static final int BOOLEAN = 9;
        public static final int STRING = 10;
        public static final int PAIR = 11;
        public static final int PARAM = 12;
        public static final int LIST = 13;


        public static String getTypeName(int type) {
            switch(type) {
                case NULL:
                    return "Null";
                case OBJECT:
                    return "Object";
                case BYTE:
                    return "Byte";
                case SHORT:
                    return "Short";
                case INT:
                    return "Integer";
                case LONG:
                    return "Long";
                case FLOAT:
                    return "Float";
                case DOUBLE:
                    return "Double";
                case BOOLEAN:
                    return "Boolean";
                case STRING:
                    return "String";
                case PAIR:
                    return "Pair";
                case PARAM:
                    return "Param";
                case LIST:
                    return "List";
                default:
                    return "";
            }
        }
    }

    public static void toJson(StringBuilder buf, Object value) {
        if (value == null) {
            return;
        }
        int type = Var.parseType(value);
        switch (type) {
            case Type.INT:
            case Type.BOOLEAN:
                buf.append(value);
                return;
            case Type.STRING: {
                buf.append("\""+value+"\"");
                return;
            }
            case Type.PARAM: {
                ((Param) value).toJson(buf);
                return;
            }
        }
    }

    public static int parseType(Object value) {
        if (value instanceof Integer) {
            return Type.INT;
        }
        if (value instanceof Boolean) {
            return Type.BOOLEAN;
        }
        if (value instanceof String) {
            return Type.STRING;
        }
        if (value instanceof Param) {
            return Type.PARAM;
        }
        return Type.OBJECT;
    }
}
